Option Strict Off
Option Explicit On
Imports VB = Microsoft.VisualBasic
Imports Microsoft.VisualBasic.PowerPacks
Namespace BMLERP
	Friend Class frmORD500
		Inherits System.Windows.Forms.Form
		Dim INVType(3) As Object
		Dim StaOrd(7) As Object
		Dim Per(3) As Object
		Dim OldEHODN As String
		Dim OldELOLN As String
		Dim NewTrans As Byte
		Dim OldCurRate As Double
		Dim oldstt As Short
		Dim oldEHSNO, oldEHCNO, oldEHDTE, oldCMNME, oldMMNME As Object
		Dim oldEHSTN As String
		Dim oldEHVAM, oldEHNOL, oldEHVAT, oldEHCPO, oldEHSTA, oldEHDUE, oldEHPTC, oldEHCER, oldEHAMT, oldEHBAM As Object
		Dim oldEHBVA As String
		Dim WithEvents RecOrd As ADODB.Recordset
		Dim SQL As String
		Sub RefeshLineAmount()
			On Error Resume Next
			Dim Amount As Object
			If txtELITN.Text = "" Then Exit Sub

			If Not ((Val(txtELQTY.Text) > 0 Or Val(txtELWGT.Text) > 0) And Val(txtELPRC.Text) > CDbl(0 & VB.Left(cboELPER.Text, 1)) <> CBool("")) Then
				Exit Sub
			End If

			Select Case cboELPER.SelectedIndex + 1
				Case 1

					Amount = Val(txtELQTY.Text) * (Val(txtELPRC.Text) - (Val(txtELPRC.Text) * Val(txtELPDC.Text) / 100))
					txtELBCA.Text = CStr(System.Math.Round(Val(Amount) * Val(txtEHCER.Text), 2))

					txtELBVA.Text = CStr(System.Math.Round(Val(txtELBCA.Text) * (Val(txtEHVAT.Text) / 100), 2))

					txtELAMT.Text = CStr(System.Math.Round(Val(txtELQTY.Text) * CDbl(Val(txtELPRC.Text) & -(Val(txtELPRC.Text) * Val(txtELPDC.Text) / 100)), 2))

					txtELVAM.Text = CStr(System.Math.Round(Val(txtELAMT.Text) * (Val(txtEHVAT.Text) / 100), 2))

				Case 2

					Amount = Val(txtELWGT.Text) * (Val(txtELPRC.Text) - (Val(txtELPRC.Text) * Val(txtELPDC.Text) / 100))
					txtELBCA.Text = CStr(System.Math.Round(Val(Amount) * Val(txtEHCER.Text), 2))

					txtELBVA.Text = CStr(System.Math.Round(Val(txtELBCA.Text) * (Val(txtEHVAT.Text) / 100), 2))

					Amount = Val(txtELWGT.Text) * CDbl(Val(txtELPRC.Text) & -(Val(txtELPRC.Text) * Val(txtELPDC.Text) / 100))

					txtELAMT.Text = CStr(System.Math.Round(Amount, 2))
					txtELVAM.Text = CStr(System.Math.Round(Val(txtELAMT.Text) * (Val(txtEHVAT.Text) / 100), 2))
			End Select
			txtELBCA.Text = VB6.Format(txtELBCA.Text, "##,##0.00")
			txtELBVA.Text = VB6.Format(txtELBVA.Text, "##,##0.00")
			txtELAMT.Text = VB6.Format(txtELAMT.Text, "##,##0.00")
			txtELVAM.Text = VB6.Format(txtELVAM.Text, "##,##0.00")
		End Sub
		Sub SaveOldValue()
			Dim oldELBVA As Object
			Dim oldELBCA As Object
			Dim oldELVAM As Object
			Dim oldELAMT As Object
			Dim oldELPDC As Object
			Dim oldELPRC As Object
			Dim oldELPSW As Object
			Dim oldELPSQ As Object
			Dim oldELWGT As Object
			Dim oldELQTY As Object
			Dim oldELITD As Object
			Dim oldELITN As Object
			Dim txtEHBAM As Object
			OldEHODN = txtEHODN.Text
			'cboEHITP.ListIndex
			'cboEHSTT.ListIndex
			oldEHDTE = txtEHDTE.Text
			oldEHCNO = txtEHCNO.Text
			oldCMNME = txtCMNME.Text
			oldEHSNO = txtEHSNO.Text
			oldMMNME = txtMMNME.Text
			oldEHSTN = cboEHSTN.defText
			oldEHSTA = txtEHSTA.Text
			oldEHDUE = txtEHDUE.Text
			oldEHCPO = txtEHCPO.Text
			oldEHPTC = txtEHPTC.Text
			oldEHVAT = txtEHVAT.Text
			'cboEHCCD = cboEHCCD
			oldEHCER = txtEHCER.Text
			oldEHNOL = txtEHNOL.Text
			oldEHAMT = txtEHAMT.Text
			oldEHVAM = txtEHVAM.Text
			oldEHBAM = txtEHBAM
			oldEHBVA = txtEHBVA.Text
			'----------------------
			OldELOLN = txtELOLN.Text
			oldELITN = txtELITN.Text
			oldELITD = txtELITD.Text
			oldELQTY = txtELQTY.Text
			oldELWGT = txtELWGT.Text
			oldELPSQ = txtELPSQ.Text
			oldELPSW = txtELPSW.Text
			oldELPRC = txtELPRC.Text
			'cboELPER = ""
			oldELPDC = txtELPDC.Text
			oldELAMT = txtELAMT.Text
			oldELVAM = txtELVAM.Text
			oldELBCA = txtELBCA.Text
			oldELBVA = txtELBVA.Text
			oldEHVAT = txtEHVAT.Text

		End Sub
		Sub RetoreOldValue()
			Dim oldELBVA As Object
			Dim oldELBCA As Object
			Dim oldELVAM As Object
			Dim oldELAMT As Object
			Dim oldELPDC As Object
			Dim oldELPRC As Object
			Dim oldELPSW As Object
			Dim oldELPSQ As Object
			Dim oldELWGT As Object
			Dim oldELQTY As Object
			Dim oldELITD As Object
			Dim oldELITN As Object
			Dim txtEHBAM As Object
			'cboELSTT.ListIndex = 0
			txtEHODN.Text = OldEHODN
			'cboEHITP.ListIndex
			'cboEHSTT.ListIndex
			txtEHDTE.Text = oldEHDTE
			txtEHCNO.Text = oldEHCNO
			txtCMNME.Text = oldCMNME
			txtEHSNO.Text = oldEHSNO
			txtMMNME.Text = oldMMNME
			cboEHSTN.defText = oldEHSTN
			txtEHSTA.Text = oldEHSTA
			txtEHDUE.Text = oldEHDUE
			txtEHCPO.Text = oldEHCPO
			txtEHPTC.Text = oldEHPTC
			txtEHVAT.Text = oldEHVAT
			'cboEHCCD = cboEHCCD
			txtEHCER.Text = oldEHCER
			txtEHNOL.Text = oldEHNOL
			txtEHAMT.Text = oldEHAMT
			txtEHVAM.Text = oldEHVAM
			txtEHBAM = oldEHBAM
			txtEHBVA.Text = oldEHBVA
			'----------------------
			txtELOLN.Text = OldELOLN
			txtELITN.Text = oldELITN
			txtELITD.Text = oldELITD
			txtELQTY.Text = oldELQTY
			txtELWGT.Text = oldELWGT
			txtELPSQ.Text = oldELPSQ
			txtELPSW.Text = oldELPSW
			txtELPRC.Text = oldELPRC
			'cboELPER = ""
			txtELPDC.Text = oldELPDC
			txtELAMT.Text = oldELAMT
			txtELVAM.Text = oldELVAM
			txtELBCA.Text = oldELBCA
			txtELBVA.Text = oldELBVA
			txtEHVAT.Text = oldEHVAT
		End Sub



		Private Sub cboEHSTN_ClickEvent(ByVal eventSender As System.Object, ByVal eventArgs As AxMSDataListLib.DDataComboEvents_ClickEvent) Handles cboEHSTN.ClickEvent
			Dim Result As Object
			On Error GoTo Err_cboEHSTN_Click
			If eventArgs.area = 1 Then
				Exit Sub
			End If
			AdoEST.ConnectionString = ConnectString
			AdoEST.ConnectionTimeout = 10
			AdoEST.CommandType = ADODB.CommandTypeEnum.adCmdText
			AdoEST.RecordSource = "Select STSTN,STCNM from EST Where STCNO='" & txtEHCNO.Text & "'"
			AdoEST.Refresh()
			If eventArgs.area = 2 Then

				Result = Find("EST", "STSAD", "WHERE STCNO='" & txtEHCNO.Text & "' AND STSTN='" & cboEHSTN.BoundText & "'")
				If Result = "" Then
					MsgBox(" Khong co so dia chi cho so ship to nay")
					cboEHSTN.Focus()
				Else
					txtEHSTA.Text = Result
					txtEHDUE.Focus()
				End If

			End If

Next_cboEHSTN_Click:
			Exit Sub
Err_cboEHSTN_Click:
			MsgBox(Err.Description)
			Resume Next_cboEHSTN_Click

		End Sub

		Private Sub cboEHSTN_KeyPressEvent(ByVal eventSender As System.Object, ByVal eventArgs As AxMSDataListLib.DDataComboEvents_KeyPressEvent) Handles cboEHSTN.KeyPressEvent
			Dim Result As Object
			On Error GoTo Err_cboEHSTN_KeyPress
			If eventArgs.keyAscii = 13 Then

				Result = Find("EST", "STSAD", "WHERE STCNO='" & txtEHCNO.Text & "' AND STSTN='" & cboEHSTN.BoundText & "'")
				If Result = "" Then
					MsgBox(" Khong co so dia chi cho so ship to nay")
					cboEHSTN.Focus()
				Else
					txtEHSTA.Text = Result
					txtEHDUE.Focus()
				End If

			End If

Next_cboEHSTN_KeyPress:
			Exit Sub
Err_cboEHSTN_KeyPress:
			MsgBox(Err.Description)
			Resume Next_cboEHSTN_KeyPress
		End Sub


		Private Sub frmORD500_Activated(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles MyBase.Activated
			'txtEHCNO.SetFocus
		End Sub

		Private Sub frmORD500_Load(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles MyBase.Load
			On Error GoTo Form_Load_err
			NewTrans = False
			ISEDITING = False
			ISADDING = False
			OpenFile()
			ButtonStatus()

			cmdNewItem.Enabled = False ' moi them vao
			Cmddelline.Enabled = False ' moi them vao
			cmdEdit.Enabled = False
			Dim myDataCaptions As String() = {"Line", "Item", "Item Description", "Quantity", "Weight", "Unit price", "Price per", "Line amount", "Base Cur Amount", "Status"}
			Dim myDataFields As String() = {"ELOLN", "ELITN", "ELITD", "ELQTY", "ELWGT", "ELPRC", "ELPER", "ELAMT", "ELBCA", "ELSTT"}
			setDataGridView(myDataCaptions, myDataFields, Me.grdECL)
Form_Load_next:
			Exit Sub
Form_Load_err:
			MsgBox("Form_Load Error : " & Err.Description)
			Resume Form_Load_next
			VB6_AddADODataBinding()
			Me.cboEHCNO.RowSource = ADOBind_AdoRCM.DataSource
			Me.cboEHSNO.RowSource = ADOBind_AdoSMM.DataSource
			Me.cboEHSTN.RowSource = ADOBind_AdoEST.DataSource
			Me.cboEHCCD.RowSource = ADOBind_AdoCCM.DataSource
			Me.cboELITN.RowSource = ADOBind_AdoIIM.DataSource
			Dim myDataCaptionserr As String() = {"Line", "Item", "Item Description", "Quantity", "Weight", "Unit price", "Price per", "Line amount", "Base Cur Amount", "Status"}
			Dim myDataFieldserr As String() = {"ELOLN", "ELITN", "ELITD", "ELQTY", "ELWGT", "ELPRC", "ELPER", "ELAMT", "ELBCA", "ELSTT"}
			setDataGridView(myDataCaptionserr, myDataFieldserr, Me.grdECL)
		End Sub
		Sub ScreenNewOrder()
			Dim txtEHBAM As Object
			On Error Resume Next
			Dim OrdNo As String
			OrdNo = GetOrdNo()
			If OrdNo = "" Then
				MsgBox("Khong Tao Duoc Ma So")
				Exit Sub
			End If
			txtEHODN.Text = OrdNo
			cboEHITP.SelectedIndex = 0
			cboEHSTT.SelectedIndex = 0
			txtEHDTE.Text = VB6.Format(WorkingDate, "dd/mm/yyyy")
			txtEHCNO.Text = ""
			txtCMNME.Text = ""
			txtEHSNO.Text = ""
			txtMMNME.Text = ""
			cboEHSTN.defText = ""
			txtEHSTA.Text = ""
			txtEHDUE.Text = ""
			txtEHCPO.Text = ""
			txtEHPTC.Text = ""
			txtEHVAT.Text = CStr(5)
			cboEHCCD.defText = ""
			txtEHCER.Text = CStr(0)
			txtEHNOL.Text = CStr(0)
			txtEHAMT.Text = CStr(0)
			txtEHVAM.Text = CStr(0)
			txtEHBAM = 0
			txtEHBVA.Text = CStr(0)
			Call SaveOldValue()
		End Sub
		Sub ScreenNewNewLine()
			On Error Resume Next
			cboELSTT.SelectedIndex = 0
			txtELOLN.Text = GetOrdLine()
			txtELITN.Text = ""
			txtELITD.Text = ""
			txtELQTY.Text = ""
			txtELWGT.Text = ""
			txtELPSQ.Text = CStr(0)
			txtELPSW.Text = CStr(0)
			txtELPRC.Text = ""
			cboELPER.Text = ""
			txtELPDC.Text = CStr(0)
			txtELAMT.Text = CStr(0)	' Line amount
			txtELVAM.Text = CStr(0)	' VAT line (VND)
			txtELBCA.Text = CStr(0)	' Base currency line amount
			txtELBVA.Text = CStr(0)	' VAT base currency

			Call SaveOldValue()
		End Sub
		Sub ScreenReview()
			On Error GoTo WriteToScreen_err
			Call ScreenReviewECH()

			Call ScreenReviewECL()
WriteToScreen_next:
			Exit Sub
WriteToScreen_err:
			MsgBox(Err.Description & " Write toscreen Function")
			Resume WriteToScreen_next
		End Sub
		Sub ScreenReviewECH()
			Dim i As Object
			On Error GoTo Check_Err
			Dim recECH As ADODB.Recordset
			recECH = DBconn.Execute("select ECH.* from ECH where ECH.EHODN='" & txtEHODN.Text & "'")
			If recECH.EOF Then
				Exit Sub
			End If
			With recECH
				' Set gia tri cho kieu order
				For i = 0 To cboEHITP.Items.Count - 1
					If VB.Left(VB6.GetItemString(cboEHITP, i), 1) = .Fields("EHITP").Value Then
						cboEHITP.SelectedIndex = i
						Exit For
					End If
				Next i
				'Set gia tri cho trang thai order
				For i = 0 To cboEHSTT.Items.Count - 1
					If VB.Left(VB6.GetItemString(cboEHSTT, i), 1) = .Fields("EHSTT").Value Then
						cboEHSTT.SelectedIndex = i
						Exit For
					End If
				Next i
				txtEHODN.Text = .Fields("EHODN").Value
				OldEHODN = txtEHODN.Text
				txtEHDTE.Text = IIf(IsDBNull(.Fields("EHDTE").Value), "", VB6.Format(.Fields("EHDTE").Value, "dd/mm/yyyy"))
				txtEHCNO.Text = .Fields("EHCNO").Value
				txtCMNME.Text = Find("RCM", "CMNME", " WHERE CMCNO='" & txtEHCNO.Text & "'")
				txtEHSNO.Text = .Fields("EHSNO").Value
				txtMMNME.Text = Find("SMM", "MMNME", " WHERE MMSNO='" & txtEHSNO.Text & "'")
				txtEHVAT.Text = CStr(.Fields("EHVAT").Value * 100)
				cboEHSTN.defText = .Fields("EHSTN").Value
				txtEHSTA.Text = .Fields("EHSTA").Value
				txtEHCPO.Text = IIf(IsDBNull(.Fields("EHCPO").Value), 0, .Fields("EHCPO").Value)
				txtEHPTC.Text = IIf(IsDBNull(.Fields("EHPTC").Value), "", .Fields("EHPTC").Value)
				txtEHDUE.Text = VB6.Format(.Fields("EHDUE").Value, "dd/mm/yyyy")
				cboEHCCD.BoundText = .Fields("EHCCD").Value
				txtEHCER.Text = .Fields("EHCER").Value
				OldCurRate = CDbl(txtEHCER.Text)
				txtEHNOL.Text = IIf(IsDBNull(.Fields("EHNOL").Value), 0, .Fields("EHNOL").Value)
				txtEHAMT.Text = .Fields("EHAMT").Value
				txtEHVAM.Text = .Fields("EHVAM").Value
				txtEHBCA.Text = .Fields("EHBAM").Value
				txtEHBVA.Text = .Fields("EHBVA").Value
				.Close()
			End With

			Call SaveOldValue()
check_err_next:
			Exit Sub
Check_Err:
			MsgBox(" ScreenReviewECH: " & Err.Description)
			Resume check_err_next
		End Sub
		Sub ScreenReviewECL()
			Dim OldELODN As Object
			Dim i As Object
			On Error GoTo Err_ScreenReviewECL
			If AdoECL.Recordset.EOF Then
				MsgBox("No Item Review or Line No invalid")
				Exit Sub
			End If

			If AdoECL.Recordset.EOF Then
				Exit Sub
			End If
			With AdoECL.Recordset
				For i = 0 To cboELSTT.Items.Count - 1
					If VB.Left(VB6.GetItemString(cboELSTT, i), 1) = .Fields("ELSTT").Value Then
						cboELSTT.SelectedIndex = i
						Exit For
					End If
				Next i
				txtELOLN.Text = .Fields("ELOLN").Value
				OldELODN = .Fields("ELOLN").Value
				txtELITN.Text = .Fields("ELITN").Value
				txtELITD.Text = .Fields("ELITD").Value
				txtELQTY.Text = .Fields("ELQTY").Value
				txtELPSQ.Text = IIf(IsDBNull(.Fields("ELPSQ").Value), 0, .Fields("ELPSQ").Value)
				txtELWGT.Text = .Fields("ELWGT").Value
				txtELPSW.Text = IIf(IsDBNull(.Fields("ELPSW").Value), 0, .Fields("ELPSW").Value)
				txtELPRC.Text = .Fields("ELPRC").Value
				For i = 0 To cboELPER.Items.Count - 1
					If VB.Left(VB6.GetItemString(cboELPER, i), 1) = .Fields("ELPER").Value Then
						cboELPER.SelectedIndex = i
						Exit For
					End If
				Next i
				txtELPDC.Text = IIf(IsDBNull(.Fields("ELPDC").Value), 0, .Fields("ELPDC").Value)
				txtELAMT.Text = .Fields("ELAMT").Value ' Line amount
				txtELVAM.Text = .Fields("ELVAM").Value ' VAT line (VND)
				txtELBCA.Text = .Fields("ELBCA").Value ' Base currency line amount
				txtELBVA.Text = .Fields("ELBVA").Value ' VAT base currency
				'.Close
			End With

			Call SaveOldValue()
Next_ScreenReviewECL:
			Exit Sub
Err_ScreenReviewECL:
			MsgBox(Err.Description)
			Resume Next_ScreenReviewECL
		End Sub


		Sub WriteToScreen()
			On Error GoTo WriteToScreen_err
			Dim recECL As ADODB.Recordset

			If NewTrans Then

				Call ScreenNewOrder()
			End If
			If ISADDING Then
				'AdoCCM.Refresh
				'AdoECL.Refresh
				Call ScreenNewNewLine()
			End If
			If ISEDITING Then 'Sua chua du lieu
				'AdoCCM.Refresh
				'AdoECL.Refresh
				Call ScreenReview()
			End If


WriteToScreen_next:
			Exit Sub
WriteToScreen_err:
			MsgBox(Err.Description & " Write toscreen Function")
			Resume WriteToScreen_next
		End Sub

		Function GetOrdLine() As Object
			Dim rstECL As ADODB.Recordset
			On Error GoTo GetOrdLine_err
			rstECL = New ADODB.Recordset
			SQL = "SELECT max(ELOLN)AS LineMax FROM ECL WHERE ELODN= '" & Trim(txtEHODN.Text) & "'"
			rstECL.Open(SQL, ConnectString, ADODB.CursorTypeEnum.adOpenKeyset, ADODB.LockTypeEnum.adLockOptimistic)
			If rstECL.RecordCount <> 0 And (rstECL.EOF) Then
				'GetOrdLine = rstECL!LineMax + 1
				GetOrdLine = rstECL.Fields("LineMax").Value + 1
			Else
				'If IsDBNull(rstECL!LineMax) Then
				If IsDBNull(rstECL.Fields("LineMax").Value) Then
					GetOrdLine = 1
				Else
					'GetOrdLine = rstECL!LineMax + 1
					GetOrdLine = rstECL.Fields("LineMax").Value + 1
				End If
			End If
			rstECL.Close()
GetOrdLine_next:
			Exit Function
GetOrdLine_err:
			MsgBox("GetOrdLine Error : " & Err.Description)
			Resume GetOrdLine_next
		End Function


		Function GetOrdNo() As Object
			'Lay so tiep theo
			On Error GoTo GetOrdNo_err

			Dim SQL As Object
			Dim sdate As Object
			Dim PrefixOrd, OrdNext As Object
			sdate = VB6.Format(Year(Now) & "/" & Month(Now) & "/01", "yyyy/mm/dd hh:mm:ss")
			If Now = sdate Then
				GetOrdNo = "I" & VB6.Format(Now, "yy") & VB6.Format(Now, "mm") & "01"
				Exit Function
			End If

			SQL = "SELECT MAX(Substring(EHODN,6,Len(EHODN))+1) as CurOrdNo FROM ECH" & " WHERE EHDTE >= '" & sdate & "'"
			Dim RecCountLine As ADODB.Recordset
			RecCountLine = DBconn.Execute(SQL)
			If RecCountLine.EOF Then
				Exit Function
			End If
			If IsDBNull(RecCountLine.Fields("CurOrdNO").Value) Then
				GetOrdNo = "I" & VB6.Format(Now, "yy") & VB6.Format(Now, "mm") & "001"
				Exit Function
			End If

			OrdNext = Val(RecCountLine.Fields("CurOrdNO").Value)
			RecCountLine.Close()

			PrefixOrd = "I" & VB6.Format(Now, "yy") & VB6.Format(Now, "mm")

			If OrdNext >= 1 And OrdNext <= 9 Then
				GetOrdNo = PrefixOrd & "00" & (OrdNext)
			ElseIf OrdNext > 9 And OrdNext <= 99 Then
				GetOrdNo = PrefixOrd & "0" & (OrdNext)
			Else
				GetOrdNo = PrefixOrd & (OrdNext)
			End If

GetOrdNo_next:
			Exit Function
GetOrdNo_err:
			MsgBox("GetOrdNo Error : " & Err.Description)
			Resume GetOrdNo_next

		End Function

		'Function GetOrdNo() 'Lay truc tiep tu database
		'    On Error GoTo GetOrdNo_err:
		'
		'    Dim SQL As String
		'    SQL = "select dbo.fn_ORDRetrieveOrdNo('ORD')as OrdNo"
		'    Dim rstOrdNo As Recordset
		'    Set rstOrdNo = DBconn.Execute(SQL)
		'    If rstOrdNo.EOF Then
		'        GetOrdNo = 0
		'        MsgBox "Khong Tao Duoc Order Number"
		'        Exit Function
		'    Else
		'        GetOrdNo = rstOrdNo!OrdNo
		'        DBconn.Execute ("Update ZSN Set Sothutu=Sothutu+1 Where maloai='ORD'")
		'        Set rstOrdNo = DBconn.Execute("select sothutu from ZSN where Maloai='ORD'")
		'        oldstt = rstOrdNo!sothutu
		'        rstOrdNo.Close
		'    End If
		'
		'GetOrdNo_next:
		'    Exit Function
		'GetOrdNo_err:
		'    MsgBox "GetOrdNo Error : " & Err.Description
		'    Resume GetOrdNo_next:
		'
		'End Function
		Sub UpdateNOLine()
			On Error GoTo UpdateNOLine_err
			Dim SQLstr As Object
			Dim RecCountLine As ADODB.Recordset
			RecCountLine = DBconn.Execute("SELECT ELODN, COUNT(ELOLN) AS CountLine From ECL " & " WHERE (ELODN = '" & txtEHODN.Text & "') AND (ELSTT <> N'7')GROUP BY ELODN")
			txtEHNOL.Text = RecCountLine.Fields("CountLine").Value
			RecCountLine.Close()
			SQLstr = "Update ECH set EHNOL='" & txtEHNOL.Text & "' WHERE EHODN='" & txtEHODN.Text & "'"
			DBconn.Execute(SQLstr)
UpdateNOLine_next:
			Exit Sub
UpdateNOLine_err:
			MsgBox("UpdateNOLine Sub: " & Err.Description)
			Resume UpdateNOLine_next

		End Sub
		Sub WritetoECH(ByRef InputType As String)
			On Error GoTo WritetoECH_err
			Dim Objcmd As ADODB.Command
			Objcmd = New ADODB.Command
			Objcmd.CommandType = ADODB.CommandTypeEnum.adCmdStoredProc
			Objcmd.ActiveConnection = DBconn
			Objcmd.CommandText = "sp_Ord_Write_ECH"
			'Objcmd.Parameters.Refresh
			Objcmd.Parameters(1).Value = InputType
			Objcmd.Parameters(2).Value = txtEHODN.Text
			Objcmd.Parameters(3).Value = VB6.Format(txtEHDTE.Text, "dd/mm/yyyy")
			Objcmd.Parameters(4).Value = cboEHITP.SelectedIndex + 1
			Objcmd.Parameters(5).Value = txtEHCNO.Text
			Objcmd.Parameters(6).Value = cboEHSTN.defText
			Objcmd.Parameters(7).Value = txtEHPTC.Text
			Objcmd.Parameters(8).Value = txtEHSTA.Text
			Objcmd.Parameters(9).Value = txtEHSNO.Text
			Objcmd.Parameters(10).Value = txtEHCPO.Text
			Objcmd.Parameters(11).Value = cboEHCCD.BoundText
			Objcmd.Parameters(12).Value = txtEHCER.Text
			Objcmd.Parameters(13).Value = txtEHNOL.Text
			Objcmd.Parameters(14).Value = IIf(txtEHVAT.Text = "", 0.05, CDbl(txtEHVAT.Text) / 100)
			Objcmd.Parameters(15).Value = VB6.Format(Now, "yyyy-mm-dd hh:mm:ss")
			Objcmd.Parameters(16).Value = IIf(txtEHDUE.Text = "", System.DBNull.Value, VB6.Format(txtEHDUE.Text, "dd/mm/yyyy"))
			Objcmd.Parameters(17).Value = "E"

			' Execute once and display...
			Objcmd.Execute()
WritetoECH_next:
			Exit Sub
WritetoECH_err:
			MsgBox(Err.Description)
			Resume WritetoECH_next
		End Sub
		Sub WritetoECL(ByRef InputType As String)
			On Error GoTo WritetoECL_err
			Dim Objcmd As ADODB.Command
			Objcmd = New ADODB.Command
			Objcmd.CommandType = ADODB.CommandTypeEnum.adCmdStoredProc
			Objcmd.ActiveConnection = DBconn
			Objcmd.CommandText = "sp_Ord_Write_ECL"
			'Objcmd.Parameters.Refresh
			Objcmd.Parameters(1).Value = InputType
			Objcmd.Parameters(2).Value = txtEHODN.Text
			Objcmd.Parameters(3).Value = Val(txtELOLN.Text)
			Objcmd.Parameters(4).Value = txtELITN.Text
			Objcmd.Parameters(5).Value = txtELITD.Text
			Objcmd.Parameters(6).Value = ""
			Objcmd.Parameters(7).Value = ""
			Objcmd.Parameters(8).Value = txtELQTY.Text
			Objcmd.Parameters(9).Value = txtELWGT.Text
			Objcmd.Parameters(10).Value = txtELPRC.Text
			Objcmd.Parameters(11).Value = cboELPER.SelectedIndex + 1
			Objcmd.Parameters(12).Value = txtELPDC.Text
			Objcmd.Parameters(13).Value = Val(txtEHVAT.Text) / 100
			Objcmd.Parameters(14).Value = txtELAMT.Text
			Objcmd.Parameters(15).Value = txtELVAM.Text
			Objcmd.Parameters(16).Value = txtELBCA.Text
			Objcmd.Parameters(17).Value = txtELBVA.Text
			Objcmd.Parameters(18).Value = txtELPSQ.Text
			Objcmd.Parameters(19).Value = txtELPSW.Text
			Objcmd.Parameters(20).Value = VB6.Format(Now, "yyyy-mm-dd hh:mm:ss")
			Objcmd.Parameters(21).Value = "N"
			Objcmd.Parameters(22).Value = UserLogin
			Objcmd.Parameters(23).Value = cboELSTT.SelectedIndex + 1

			' Execute once and display...
			Objcmd.Execute()
WritetoECL_next:
			Exit Sub
WritetoECL_err:
			MsgBox(Err.Description)
			Resume WritetoECL_next

		End Sub

		Function Check() As Object
			Dim MsgResult As Object
			Dim recECH_ECL As Object
			Dim rst As Object
			On Error GoTo Check_Err
			Check = True
			If NewTrans Then ' nhap moi them vao ECH
				rst = New ADODB.Recordset
				SQL = "SELECT EHODN FROM ECH WHERE EHODN= '" & Trim(txtEHODN.Text) & "'"
				rst.Open(SQL, ConnectString, ADODB.CursorTypeEnum.adOpenKeyset, ADODB.LockTypeEnum.adLockOptimistic)
				If rst.RecordCount <> 0 Then
					MsgBox("The Order " & Trim(txtEHODN.Text) & " is Already exist")
					Check = False
					txtEHODN.Focus()
					Exit Function
				End If
				'Kiem tra status khi sua chua du lieu
			ElseIf ISEDITING Then
				SQL = "SELECT EHSTT,ELSTT FROM ECH JOIN ECL ON ECH.EHODN=ECL.ELODN WHERE EHODN= '" & Trim(txtEHODN.Text) & "'" & " AND ECL.ELOLN='" & txtELOLN.Text & "'"
				recECH_ECL = DBconn.Execute(SQL)
				'rst.Open SQL, ConnectString, adOpenKeyset, adLockOptimistic
				If recECH_ECL.RecordCount <> 0 Then

					If recECH_ECL!EHSTT = 6 Then ' kiem tra toan bo Order
						MsgResult = MsgBox("This Order has finished.You can't change data", MsgBoxStyle.OkOnly, "Warning")
						Check = False
						Exit Function
					End If
					If recECH_ECL!ELSTT = 6 Then
						MsgResult = MsgBox("This item has finished.You can't change data", MsgBoxStyle.OkOnly, "Warning")
						Check = False
						Exit Function

					End If
				End If
			End If

			If txtEHODN.Text = "" Then
				MsgBox("You must enter the Order Number", MsgBoxStyle.OkOnly, "Error entry")
				Check = False
				txtEHODN.Focus()
				Exit Function
			End If
			' Kiem tra ma khach hang
			If txtEHCNO.Text = "" Then
				MsgBox("You must enter Customer No", MsgBoxStyle.OkOnly, "Error entry")
				Check = False
				txtEHCNO.Focus()
				Exit Function
			End If
			'kiem tra ngay Order
			If (txtEHDTE.Text = "") Or (Not IsDate(txtEHDTE.Text)) Then
				MsgBox("You must enter Order Date", MsgBoxStyle.OkOnly, "Error entry")
				Check = False
				txtEHDTE.Focus()
				Exit Function
			End If
			'kiem tra Currency code
			If (cboEHCCD.BoundText = "") Then
				MsgBox("You must fill Currency code", MsgBoxStyle.OkOnly, "Error entry")
				cboEHCCD.Focus()
				Check = False
				Exit Function
			End If
			'kiem tra Exchange rate
			If (txtEHCER.Text = "") Or Not IsNumeric(txtEHCER.Text) Then
				MsgBox("You must fill exchange rate", MsgBoxStyle.OkOnly, "Error entry")
				Check = False
				txtEHCER.Focus()
				Exit Function
			End If
			If (txtELQTY.Text = "") Or (txtELPSQ.Text = "") Or (txtELWGT.Text = "") Or (txtELPSW.Text = "") Then
				MsgBox("You must enter values in Quantity and Weight textbox", MsgBoxStyle.OkOnly, "Error entry")
				Check = False
				txtELQTY.Focus()
				Exit Function
			End If
			If Not (IsNumeric(txtELQTY.Text) And IsNumeric(txtELPSQ.Text) And IsNumeric(txtELWGT.Text) And IsNumeric(txtELPSW.Text)) Then
				MsgBox("You must enter number values in Quantity and Weight textbox", MsgBoxStyle.OkOnly, "Error entry")
				Check = False
				txtELQTY.Focus()
				Exit Function
			End If
			'kiem tra unit price
			If (txtELPRC.Text = "") Or Not IsNumeric(txtELPRC.Text) Then
				MsgBox("You must fill unit price textbox", MsgBoxStyle.OkOnly, "Error entry")
				Check = False
				txtELPRC.Focus()
				Exit Function
			End If
			'kiem tra tinh theo kl hay so luong
			If (cboELPER.Text = "") Then
				MsgBox("You must fill price per textbox", MsgBoxStyle.OkOnly, "Error entry")
				Check = False
				cboELPER.Focus()
				Exit Function
			End If
			'kiem tra VAT
			If (txtEHVAT.Text = "") Then
				MsgBox("You must enter VAT textbox", MsgBoxStyle.OkOnly, "Error entry")
				Check = False
				txtEHVAT.Focus()
				Exit Function
			End If
Check_next:
			Exit Function
Check_Err:
			MsgBox("Check function: " & Err.Description)
			Resume Check_next
		End Function
		Private Sub cboEHCCD_ClickEvent(ByVal eventSender As System.Object, ByVal eventArgs As AxMSDataListLib.DDataComboEvents_ClickEvent) Handles cboEHCCD.ClickEvent
			Dim Result As Object '01
			On Error GoTo Err_cboEHCCD_Click
			If eventArgs.area = 1 Then
				Exit Sub
			End If
			AdoCCM.ConnectionString = ConnectString
			AdoCCM.ConnectionTimeout = 10
			AdoCCM.CommandType = ADODB.CommandTypeEnum.adCmdText
			AdoCCM.RecordSource = "Select * from CCM"
			AdoCCM.Refresh()
			If eventArgs.area = 2 Then

				Result = Find("CCI", "CICCR", "WHERE CICCD='" & cboEHCCD.BoundText & "'")
				If Result = "" Then
					MsgBox(" Can not find Exchange rate for currency code")
					cboEHCCD.Focus()
				Else
					txtEHCER.Text = Result
					txtEHCER.Focus()
				End If

			End If

Next_cboEHCCD_Click:
			Exit Sub
Err_cboEHCCD_Click:
			MsgBox(Err.Description)
			Resume Next_cboEHCCD_Click
		End Sub

		Private Sub cboEHCCD_KeyPressEvent(ByVal eventSender As System.Object, ByVal eventArgs As AxMSDataListLib.DDataComboEvents_KeyPressEvent) Handles cboEHCCD.KeyPressEvent
			Dim Result As Object
			On Error GoTo Err_action
			If eventArgs.keyAscii = 13 Then
				Result = Find("CCI", "CICCR", "WHERE CICCD='" & cboEHCCD.BoundText & "'")
				If Result = "" Then
					MsgBox(" Can not find Exchange rate for currency code")
					cboEHCCD.Focus()
				Else
					txtEHCER.Text = Result
					txtEHCER.Focus()
				End If
			End If

Next_Action:
			Exit Sub
Err_action:
			MsgBox(Err.Description)
			Resume Next_Action
		End Sub


		Private Sub cboEHCNO_ClickEvent(ByVal eventSender As System.Object, ByVal eventArgs As AxMSDataListLib.DDataComboEvents_ClickEvent) Handles cboEHCNO.ClickEvent
			If eventArgs.area = 1 Then
				Exit Sub
			End If


			On Error GoTo Err_action
			'AdoRCM.Refresh
			If eventArgs.area = 2 Then
				txtEHCNO.Text = cboEHCNO.BoundText
				txtCMNME.Text = cboEHCNO.CtlText
				cboEHCNO.Visible = False
				txtEHSNO.Focus()
			End If
Next_Action:
			Exit Sub
Err_action:
			MsgBox(Err.Description)
			Resume Next_Action
		End Sub

		Private Sub cboEHCNO_Leave(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cboEHCNO.Leave
			On Error GoTo Err_action
			'AdoRCM.Refresh
			If cboEHCNO.BoundText = "" Then
				MsgBox("Ban phai chon mot ma khach hang")
				'cboEHCNO.SetFocus
				Exit Sub
			End If

			txtEHCNO.Text = cboEHCNO.BoundText
			txtCMNME.Text = cboEHCNO.CtlText
			cboEHCNO.Visible = False


Next_Action:
			Exit Sub
Err_action:
			MsgBox(Err.Description)
			Resume Next_Action
		End Sub


		Private Sub cboEHITP_SelectedIndexChanged(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cboEHITP.SelectedIndexChanged
			'    txtEHODN.SetFocus
		End Sub

		Private Sub cboEHSNO_ClickEvent(ByVal eventSender As System.Object, ByVal eventArgs As AxMSDataListLib.DDataComboEvents_ClickEvent) Handles cboEHSNO.ClickEvent
			On Error GoTo Err_action
			'AdoSMM.Refresh
			If eventArgs.area = 2 Then
				txtEHSNO.Text = cboEHSNO.BoundText
				txtMMNME.Text = cboEHSNO.CtlText
				cboEHSNO.Visible = False
				cboEHSTN.Focus()
			End If
Next_Action:
			Exit Sub
Err_action:
			MsgBox(Err.Description)
			Resume Next_Action

		End Sub

		Private Sub cboEHSTT_SelectedIndexChanged(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cboEHSTT.SelectedIndexChanged

			'MsgBox Left(cboEHSTT.List(cboEHSTT.ListIndex), 1)
		End Sub

		Private Sub cboELITN_ClickEvent(ByVal eventSender As System.Object, ByVal eventArgs As AxMSDataListLib.DDataComboEvents_ClickEvent) Handles cboELITN.ClickEvent
			On Error GoTo Err_action
			'AdoIIM.Refresh
			If eventArgs.area = 2 Then
				txtELITN.Text = cboELITN.BoundText
				txtELITD.Text = Find("IIM", "IMETD", " WHERE IMITN='" & txtELITN.Text & "'")
				txtELPRC.Text = Find("IIM", "IMPRC", "WHERE IMITN='" & txtELITN.Text & "'")
				cboELITN.Visible = False
				cmdSave.Enabled = True
				txtELQTY.Focus()

			End If
Next_Action:
			Exit Sub
Err_action:
			MsgBox(Err.Description)
			Resume Next_Action

		End Sub
		Private Sub cboELITN_KeyPressEvent(ByVal eventSender As System.Object, ByVal eventArgs As AxMSDataListLib.DDataComboEvents_KeyPressEvent) Handles cboELITN.KeyPressEvent
			On Error GoTo Err_action

			If eventArgs.keyAscii <> 13 Then
				MsgBox("Phai chon mot item")
				cboELITN.Focus()
				Exit Sub
			End If
			txtELITN.Text = cboELITN.BoundText
			txtELITD.Text = Find("IIM", "IMETD", " WHERE IMITN='" & txtELITN.Text & "'")
			txtELPRC.Text = Find("IIM", "IMPRC", "WHERE IMITN='" & txtELITN.Text & "'")
			cboELITN.Visible = False
			cmdSave.Enabled = True
			txtELQTY.Focus()

Next_Action:
			Exit Sub
Err_action:
			MsgBox(Err.Description)
			Resume Next_Action
		End Sub

		Private Sub cboELITN_Leave(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cboELITN.Leave
			On Error GoTo Err_action
			txtELITN.Text = cboELITN.BoundText
			If txtELITN.Text = "" Then
				MsgBox("Phai chon mot item")
				'cboELITN.SetFocus
				Exit Sub
			End If
			txtELPRC.Text = Find("IIM", "IMPRC", "WHERE IMITN='" & txtELITN.Text & "'")
			txtELITD.Text = Find("IIM", "IMITD", " WHERE IMITN='" & txtELITN.Text & "'")
			cboELITN.Visible = False
			cmdSave.Enabled = True
			'txtELVAT = VATRate(txtEHCNO, txtELITN)
			'txtELQTY.SetFocus
Next_Action:
			Exit Sub
Err_action:
			MsgBox(Err.Description)
			Resume Next_Action
		End Sub

		Private Sub cboELPER_SelectedIndexChanged(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cboELPER.SelectedIndexChanged

			Dim DiscAMT, DiscBAM As Object
			On Error GoTo Err_action
			Call RefeshLineAmount()
			'Select Case cboELPER.ListIndex + 1
			'    Case 1
			'
			'        txtELBCA = Val(txtELQTY) * (Val(txtELPRC) - (Val(txtELPRC) * Val(txtELPDC) / 100))
			'        txtELBVA = Val(txtELBCA) * Val(txtEHVAT)
			'
			'        txtELAMT = Val(txtELQTY) * (Val(txtELPRC) - (Val(txtELPRC) * Val(txtELPDC) / 100)) * Val(txtEHCER)
			'        txtELVAM = Val(txtELAMT) * Val(txtEHVAT)
			'
			'    Case 2
			'
			'        txtELBCA = Val(txtELWGT) * (Val(txtELPRC) - (Val(txtELPRC) * Val(txtELPDC) / 100))
			'        txtELBVA = Val(txtELBCA) * Val(txtEHVAT)
			'
			'        txtELAMT = Val(txtELWGT) * (Val(txtELPRC) - (Val(txtELPRC) * Val(IIf(txtELPDC = "", 0, txtELPDC)) / 100)) * Val(txtEHCER)
			'        txtELVAM = Val(txtELAMT) * Val(txtEHVAT)
			'End Select

Next_Action:
			Exit Sub
Err_action:
			MsgBox(Err.Description)
			Resume Next_Action

			'txtELVAT.SetFocus
		End Sub

		Private Sub cboELPER_KeyPress(ByVal eventSender As System.Object, ByVal eventArgs As System.Windows.Forms.KeyPressEventArgs) Handles cboELPER.KeyPress
			Dim KeyAscii As Short = Asc(eventArgs.KeyChar)
			On Error GoTo Err_action
			If KeyAscii <> 13 Then
				GoTo EventExitSub
			End If
			Select Case VB.Left(cboELPER.Text, 1)
				Case "1"
					cboELPER.SelectedIndex = 0
					cmdSave.Focus()
				Case "2"
					cboELPER.SelectedIndex = 1
					cmdSave.Focus()
				Case Else
					MsgBox("Nhap lai cach tinh gia : 1-tinh theo soluong; 2-tinh theo khoi luong")
					cboELPER.Focus()
			End Select

Next_Action:
			GoTo EventExitSub
Err_action:
			MsgBox(Err.Description)
			Resume Next_Action

EventExitSub:
			eventArgs.KeyChar = Chr(KeyAscii)
			If KeyAscii = 0 Then
				eventArgs.Handled = True
			End If
		End Sub
		Private Sub cboELPER_Leave(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cboELPER.Leave
			On Error GoTo Err_action
			Select Case VB.Left(cboELPER.Text, 1)
				Case "1"
					cboELPER.SelectedIndex = 0
				Case "2"
					cboELPER.SelectedIndex = 1
				Case Else
					MsgBox("Nhap lai cach tinh gia : 1-tinh theo soluong; 2-tinh theo khoi luong")
			End Select
Next_Action:
			Exit Sub
Err_action:
			MsgBox(Err.Description)
			Resume Next_Action
		End Sub
		Private Sub cmdReview_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdReview.Click
			On Error GoTo Err_action
			'Chuyen ten form dung .tag
			'Dim myform As New frmReview
			'myform.Tag = "ORD500"
			'myform.Show()
			frmReview.Tag = "ORD500"
			frmReview.ShowDialog()
			NewTrans = False
			ISEDITING = True
			ISADDING = False
			'If myform.Tag = "" Then
			If frmReview.Tag = "" Then
				Exit Sub
			End If
			'txtEHODN.Text = myform.Tag
			'myform.Close()
			txtEHODN.Text = frmReview.Tag
			frmReview.Close()
			'txtELOLN = 1
			AdoECL.RecordSource = "Select ECL.* from ECL " & " LEFT JOIN ECH ON ECL.ELODN=ECH.EHODN where ECH.EHODN ='" & txtEHODN.Text & "' ORDER BY ELOLN"
			AdoECL.Refresh()
			WriteToScreen()
			cmdEdit.Enabled = True
			cmdAddNew.Enabled = True
			cmdNewItem.Enabled = True
			Cmddelline.Enabled = True
			FrameBB.Enabled = False
			FrameECLine.Enabled = False
			Call SaveOldValue()
Next_Action:
			Exit Sub
Err_action:
			MsgBox(Err.Description)
			Resume Next_Action
		End Sub


		Private Sub cmdAddnew_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdAddNew.Click

			On Error GoTo addnew_err
			'OpenFile
			NewTrans = True
			ISADDING = True
			ISEDITING = False
			WriteToScreen()
			'ButtonStatus
			FrameBB.Enabled = True
			FrameECLine.Enabled = True
			cmdReview.Enabled = False
			cmdAddNew.Enabled = False
			cmdNewItem.Enabled = False
			cmdEdit.Enabled = False
			Cmddelline.Enabled = False
			cmdSave.Enabled = False
			cmdCancel.Enabled = True
			txtEHCNO.Focus()
			'AdoCCM.Refresh
			'If Me.AdoRCM.EOF <> True Then
			'Me.AdoRCM = New VB6.ADODC
			'Me.AdoRCM.ConnectionString = ConnectString
			'Me.AdoRCM.ConnectionTimeout = 10
			'Me.AdoRCM.CommandType = ADODB.CommandTypeEnum.adCmdText
			'Me.AdoRCM.RecordSource = "Select CMCNO,CMNME from RCM" & " ORDER BY CMNME"
			'End If
			'If AdoSMM.EOF Then
			'AdoSMM.ConnectionString = ConnectString
			'AdoSMM.ConnectionTimeout = 10
			'AdoSMM.CommandType = ADODB.CommandTypeEnum.adCmdText
			'AdoSMM.RecordSource = "Select * from SMM"
			'End If
			AdoRCM.Refresh()
			AdoSMM.Refresh()
			'AdoEST.Refresh
addnew_next:
			Exit Sub
addnew_err:
			MsgBox(Err.Description)
			Resume addnew_next
		End Sub
		Private Sub cmdNewItem_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdNewItem.Click
			On Error GoTo Err_action
			'NewTrans = True
			'OpenFile
			If txtEHODN.Text = "" Then
				Exit Sub
			End If

			NewTrans = False
			ISADDING = True
			ISEDITING = False
			WriteToScreen()
			'ButtonStatus
			FrameBB.Enabled = True
			FrameECLine.Enabled = True
			cmdReview.Enabled = False
			cmdAddNew.Enabled = False
			cmdNewItem.Enabled = False
			cmdEdit.Enabled = False
			Cmddelline.Enabled = False
			cmdSave.Enabled = True
			cmdCancel.Enabled = True
			txtELITN.Focus()

Next_Action:
			Exit Sub
Err_action:
			MsgBox(Err.Description)
			Resume Next_Action

		End Sub
		Private Sub cmdEdit_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdEdit.Click
			'OpenFile
			On Error GoTo Err_action
			NewTrans = False
			ISADDING = False
			ISEDITING = True
			'WriteToScreen
			'ButtonStatus
			cmdReview.Enabled = False
			cmdAddNew.Enabled = False
			cmdNewItem.Enabled = False
			cmdEdit.Enabled = False
			Cmddelline.Enabled = False
			cmdSave.Enabled = True
			cmdCancel.Enabled = True
			FrameBB.Enabled = True
			FrameECLine.Enabled = True
			Call SaveOldValue()
Next_Action:
			Exit Sub
Err_action:
			MsgBox(Err.Description)
			Resume Next_Action

		End Sub

		Private Sub cmdCancel_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdCancel.Click
			On Error GoTo Err_action
			Dim rsttemp As ADODB.Recordset
			If NewTrans Then
				txtEHODN.Text = ""
				'    Set rsttemp = DBconn.Execute("select sothutu from ZSN where Maloai='ORD'")
				'    If rsttemp!sothutu = oldstt Then
				'        DBconn.Execute ("Update ZSN Set Sothutu=Sothutu-2")
				'    End If
			End If
			'NewTrans = False
			'ISADDING = False
			'ISEDITING = False
			Call RetoreOldValue()
			'ButtonStatus
			FrameBB.Enabled = False
			FrameECLine.Enabled = False
			If NewTrans Then
				cmdReview.Enabled = True
				cmdAddNew.Enabled = True
				cmdNewItem.Enabled = False
				cmdEdit.Enabled = False
				Cmddelline.Enabled = False
				cmdSave.Enabled = False
				cmdCancel.Enabled = False
				Exit Sub
			End If
			If ISEDITING Then
				cmdReview.Enabled = True
				cmdAddNew.Enabled = True
				cmdNewItem.Enabled = True
				cmdEdit.Enabled = True
				Cmddelline.Enabled = True
				cmdSave.Enabled = False
				cmdCancel.Enabled = False
				Exit Sub
			End If
			If ISADDING Then
				cmdReview.Enabled = True
				cmdAddNew.Enabled = True
				cmdNewItem.Enabled = True
				cmdEdit.Enabled = True
				Cmddelline.Enabled = True
				cmdSave.Enabled = False
				cmdCancel.Enabled = False
				Exit Sub
			End If
Next_Action:
			Exit Sub
Err_action:
			MsgBox(Err.Description)
			Resume Next_Action

		End Sub

		Private Sub cmddelline_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles Cmddelline.Click
			On Error GoTo Err_action
			cmdReview.Enabled = True
			cmdAddNew.Enabled = True
			cmdNewItem.Enabled = True
			cmdEdit.Enabled = True
			Cmddelline.Enabled = True
			cmdSave.Enabled = False
			cmdCancel.Enabled = False

			Dim Result As String
			Result = Find("ECH", "EHODN", "WHERE EHODN='" & txtEHODN.Text & "'" & " AND EHSTT='1'")
			If Result = "" Then
				MsgBox(" Order nay da bi khoa")
				Exit Sub
			End If

			If txtEHODN.Text <> "" And txtELOLN.Text <> "" Then
				' SQL = "delete from ECL where ELOLN='" & Trim(txtELOLN.Text) & "'" & " AND ELSTT = '1'" & " AND ELOLN='" & Trim(txtELOLN.Text) & "'"

				If MsgBox(" Do you want delete this line ?", MsgBoxStyle.DefaultButton2 + MsgBoxStyle.OkCancel + MsgBoxStyle.Critical, "Deleting...") = MsgBoxResult.Ok Then
					'DBconn.Execute (SQL)

					With AdoECL.Recordset
						.Delete()
						.MoveNext()
						If .EOF Then .MoveLast()
					End With
					ScreenReviewECL()
					Exit Sub

					'AdoECL.Refresh
				Else
					Exit Sub
				End If

			Else
				MsgBox("The TextBox OrderNo or ItemNo is Empty...!", MsgBoxStyle.Critical, "Warning")
			End If

Next_Action:
			Exit Sub
Err_action:
			MsgBox("Can not delete this line !", MsgBoxStyle.Critical, "Warning")
			Resume Next_Action

		End Sub






		Private Sub cmdSave_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdSave.Click

			On Error GoTo Err_action
			If Check() = False Then
				MsgBox("Loi nhap lieu")
				Exit Sub
			End If

			''
			''    If Not ValidInput() Then Exit Sub

			WriteToFile()
			AdoECL.RecordSource = "Select ECL.* from ECL " & " LEFT JOIN ECH ON ECL.ELODN=ECH.EHODN where ECH.EHODN ='" & txtEHODN.Text & "' ORDER BY ELOLN"
			AdoECL.Refresh()
			FrameBB.Enabled = False
			FrameECLine.Enabled = False
			If NewTrans Then
				cmdReview.Enabled = True
				cmdAddNew.Enabled = True
				cmdNewItem.Enabled = True
				cmdEdit.Enabled = True
				Cmddelline.Enabled = False
				cmdSave.Enabled = False
				cmdCancel.Enabled = False
				Exit Sub
			End If
			If ISEDITING Then
				cmdReview.Enabled = True
				cmdAddNew.Enabled = True
				cmdNewItem.Enabled = True
				cmdEdit.Enabled = True
				Cmddelline.Enabled = True
				cmdSave.Enabled = False
				cmdCancel.Enabled = False
				Exit Sub
			End If
			If ISADDING Then
				cmdReview.Enabled = True
				cmdAddNew.Enabled = True
				cmdNewItem.Enabled = True
				cmdEdit.Enabled = True
				Cmddelline.Enabled = True
				cmdSave.Enabled = False
				cmdCancel.Enabled = False
				Exit Sub
			End If
Next_Action:
			Exit Sub
Err_action:
			MsgBox(Err.Description)
			Resume Next_Action

		End Sub


		Private Sub cmdExit_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdExit.Click

			Me.Close()
		End Sub

		Private Sub grdECL_ClickEvent(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles grdECL.ClickEvent

			On Error GoTo Err_grdECLClick
			txtELOLN.Text = grdECL.Columns(1).Value
			NewTrans = False
			ISADDING = False
			ISEDITING = True

			WriteToScreen()
			If SELECTROW = "" Then SELECTROW = 1
			AdoECL.Recordset.AbsolutePosition = SELECTROW
Next_grdECLClick:
			Exit Sub
Err_grdECLClick:
			MsgBox(Err.Description)
			Resume Next_grdECLClick
		End Sub

		Private Sub grdECL_RowColChange(ByVal eventSender As System.Object, ByVal eventArgs As AxMSDataGridLib.DDataGridEvents_RowColChangeEvent) Handles grdECL.RowColChange
			On Error GoTo GrdECL_RowColChange_err
			SELECTROW = AdoECL.Recordset.Bookmark
GrdECL_RowColChange_next:
			Exit Sub
GrdECL_RowColChange_err:
			MsgBox("GrdECL_RowColChange Error : " & Err.Description)
			Resume GrdECL_RowColChange_next

		End Sub








		Private Sub txtEHCER_KeyPress(ByVal eventSender As System.Object, ByVal eventArgs As System.Windows.Forms.KeyPressEventArgs) Handles txtEHCER.KeyPress
			Dim KeyAscii As Short = Asc(eventArgs.KeyChar)
			On Error Resume Next
			If KeyAscii <> 13 Then
				GoTo EventExitSub
			End If
			If Val(txtEHCER.Text) < 1 Then
				MsgBox("Gia tri ngoai te khong hop le")
				txtEHCER.Focus()
				GoTo EventExitSub
			End If

			txtELITN.Focus()
			Call RefeshLineAmount()
EventExitSub:
			eventArgs.KeyChar = Chr(KeyAscii)
			If KeyAscii = 0 Then
				eventArgs.Handled = True
			End If
		End Sub

		Private Sub txtEHCER_Leave(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles txtEHCER.Leave
			If Val(txtEHCER.Text) < 1 Then
				MsgBox("Gia tri ngoai te khong hop le")
				'txtEHCER.SetFocus
				Exit Sub
			End If

			Call RefeshLineAmount()
		End Sub

		Private Sub txtEHCNO_KeyPress(ByVal eventSender As System.Object, ByVal eventArgs As System.Windows.Forms.KeyPressEventArgs) Handles txtEHCNO.KeyPress
			Dim KeyAscii As Short = Asc(eventArgs.KeyChar)
			On Error GoTo Err_action
			Dim Result As String
			If KeyAscii <> 13 Then
				GoTo EventExitSub
			End If
			Result = Find("RCM", "CMNME", "WHERE CMCNO='" & txtEHCNO.Text & "'")
			Dim rstCusInfo As ADODB.Recordset
			If Result = "" Then
				MsgBox("Khong co ma khach hang nay")
				AdoRCM.Refresh()
				cboEHCNO.Visible = True
				cboEHCNO.Focus()
				GoTo EventExitSub
			Else
				cboEHCNO.Visible = False
				txtCMNME.Text = Result
				rstCusInfo = DBconn.Execute("Select CMSTN,CMSNO,CMVAT,CMPTC from RCM where CMCNO='" & txtEHCNO.Text & "'")
				txtEHVAT.Text = CStr(100 * Find("VVR", "VRVPC", "WHERE VRITV='" & rstCusInfo.Fields("CMVAT").Value & "'" & " AND VRCVT='" & rstCusInfo.Fields("CMVAT").Value & "'"))
				txtEHSNO.Text = rstCusInfo.Fields("CMSNO").Value
				txtMMNME.Text = Find("SMM", "MMNME", "WHERE MMSNO='" & rstCusInfo.Fields("CMSNO").Value & "'")
				txtEHPTC.Text = rstCusInfo.Fields("CMPTC").Value
				cboEHSTN.defText = rstCusInfo.Fields("CMSTN").Value
				txtEHSTA.Text = Find("EST", "STSAD", "WHERE STCNO='" & txtEHCNO.Text & "'" & " AND STSTN='" & rstCusInfo.Fields("CMSTN").Value & "'")
				txtEHDUE.Focus()

			End If


Next_Action:
			GoTo EventExitSub
Err_action:
			MsgBox(Err.Description)
			Resume Next_Action

EventExitSub:
			eventArgs.KeyChar = Chr(KeyAscii)
			If KeyAscii = 0 Then
				eventArgs.Handled = True
			End If
		End Sub

		Private Sub txtEHCNO_Leave(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles txtEHCNO.Leave
			On Error GoTo Err_action
			Dim Result As String
			Result = Find("RCM", "CMNME", "WHERE CMCNO='" & txtEHCNO.Text & "'")
			Dim rstCusInfo As ADODB.Recordset
			If Result = "" Then
				MsgBox("Khong co ma khach hang nay")
				cboEHCNO.Visible = True
				'cboEHCNO.SetFocus
			Else
				cboEHCNO.Visible = False
				txtCMNME.Text = Result
				rstCusInfo = DBconn.Execute("Select CMSTN,CMSNO,CMVAT,CMPTC from RCM where CMCNO='" & txtEHCNO.Text & "'")
				txtEHVAT.Text = CStr(100 * Find("VVR", "VRVPC", "WHERE VRITV='" & rstCusInfo.Fields("CMVAT").Value & "'" & " AND VRCVT='" & rstCusInfo.Fields("CMVAT").Value & "'"))
				txtEHSNO.Text = rstCusInfo.Fields("CMSNO").Value
				txtMMNME.Text = Find("SMM", "MMNME", "WHERE MMSNO='" & rstCusInfo.Fields("CMSNO").Value & "'")
				txtEHPTC.Text = rstCusInfo.Fields("CMPTC").Value
				cboEHSTN.defText = rstCusInfo.Fields("CMSTN").Value
				txtEHSTA.Text = Find("EST", "STSAD", "WHERE STCNO='" & txtEHCNO.Text & "'" & " AND STSTN='" & rstCusInfo.Fields("CMSTN").Value & "'")
				txtEHDUE.Focus()
			End If

Next_Action:
			Exit Sub
Err_action:
			MsgBox(Err.Description)
			Resume Next_Action

		End Sub

		Private Sub txtEHCPO_KeyPress(ByVal eventSender As System.Object, ByVal eventArgs As System.Windows.Forms.KeyPressEventArgs) Handles txtEHCPO.KeyPress
			Dim KeyAscii As Short = Asc(eventArgs.KeyChar)
			If KeyAscii = 13 Then txtEHPTC.Focus()
			eventArgs.KeyChar = Chr(KeyAscii)
			If KeyAscii = 0 Then
				eventArgs.Handled = True
			End If
		End Sub

		Private Sub txtEHDTE_KeyPress(ByVal eventSender As System.Object, ByVal eventArgs As System.Windows.Forms.KeyPressEventArgs) Handles txtEHDTE.KeyPress
			Dim KeyAscii As Short = Asc(eventArgs.KeyChar)
			If KeyAscii = 13 Then txtEHCNO.Focus()
			eventArgs.KeyChar = Chr(KeyAscii)
			If KeyAscii = 0 Then
				eventArgs.Handled = True
			End If
		End Sub

		Private Sub txtEHDUE_KeyPress(ByVal eventSender As System.Object, ByVal eventArgs As System.Windows.Forms.KeyPressEventArgs) Handles txtEHDUE.KeyPress
			Dim KeyAscii As Short = Asc(eventArgs.KeyChar)
			On Error GoTo Err_action
			If KeyAscii = 13 Then
				txtEHCPO.Focus()
				If Not IsDate(txtEHDUE.Text) Then
					MsgBox("Date entry invalid")
					txtEHDUE.Focus()
				End If
			End If

Next_Action:
			GoTo EventExitSub
Err_action:
			MsgBox(Err.Description)
			Resume Next_Action

EventExitSub:
			eventArgs.KeyChar = Chr(KeyAscii)
			If KeyAscii = 0 Then
				eventArgs.Handled = True
			End If
		End Sub

		Private Sub txtEHDUE_Leave(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles txtEHDUE.Leave
			On Error GoTo Err_action
			If txtEHDUE.Text <> "" Then
				If Not IsDate(txtEHDUE.Text) Then
					MsgBox("Date entry invalid")
					'txtEHDUE.SetFocus
				End If
			End If

Next_Action:
			Exit Sub
Err_action:
			MsgBox(Err.Description)
			Resume Next_Action

		End Sub



		Private Sub txtEHODN_KeyPress(ByVal eventSender As System.Object, ByVal eventArgs As System.Windows.Forms.KeyPressEventArgs) Handles txtEHODN.KeyPress
			Dim KeyAscii As Short = Asc(eventArgs.KeyChar)
			Dim Result As Object
			On Error GoTo Err_action
			If KeyAscii = 13 Then
				Result = Find("ECH", "EHODN", "WHERE EHODN='" & txtEHODN.Text & "'")
				If Result = "" Then
					txtEHDTE.Focus()
				Else
					MsgBox("Order Number is already exist")
					txtEHODN.Text = Result
					NewTrans = False
					ISADDING = False
					ISEDITING = False
					WriteToScreen()
					ButtonStatus()
				End If
			End If

Next_Action:
			GoTo EventExitSub
Err_action:
			MsgBox(Err.Description)
			Resume Next_Action

EventExitSub:
			eventArgs.KeyChar = Chr(KeyAscii)
			If KeyAscii = 0 Then
				eventArgs.Handled = True
			End If
		End Sub

		Private Sub txtEHPTC_KeyPress(ByVal eventSender As System.Object, ByVal eventArgs As System.Windows.Forms.KeyPressEventArgs) Handles txtEHPTC.KeyPress
			Dim KeyAscii As Short = Asc(eventArgs.KeyChar)
			If KeyAscii = 13 Then
				cboEHCCD.Focus()
			End If
			eventArgs.KeyChar = Chr(KeyAscii)
			If KeyAscii = 0 Then
				eventArgs.Handled = True
			End If
		End Sub

		Private Sub txtEHSNO_KeyPress(ByVal eventSender As System.Object, ByVal eventArgs As System.Windows.Forms.KeyPressEventArgs) Handles txtEHSNO.KeyPress
			Dim KeyAscii As Short = Asc(eventArgs.KeyChar)
			On Error GoTo Err_action
			Dim Result As String
			If KeyAscii = 13 Then
				Result = Find("SMM", "MMNME", "WHERE MMSNO='" & txtEHSNO.Text & "'")
				If Result = "" Then
					MsgBox("Khong co ma nguoi ban")
					AdoSMM.Refresh()
					cboEHSNO.Visible = True
					cboEHSNO.Focus()
				Else
					cboEHSNO.Visible = False
					txtMMNME.Text = Result
					cboEHSTN.Focus()
				End If
			End If

Next_Action:
			GoTo EventExitSub
Err_action:
			MsgBox(Err.Description)
			Resume Next_Action

EventExitSub:
			eventArgs.KeyChar = Chr(KeyAscii)
			If KeyAscii = 0 Then
				eventArgs.Handled = True
			End If
		End Sub

		Private Sub txtEHSNO_Leave(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles txtEHSNO.Leave
			Dim Result As String
			Result = Find("SMM", "MMNME", "WHERE MMSNO='" & txtEHSNO.Text & "'")
			If Result = "" Then
				'MsgBox "Khong co ma nguoi ban nay"
				cboEHSNO.Visible = True
				'cboEHSNO.SetFocus
			Else
				cboEHSNO.Visible = False
				txtMMNME.Text = Result
			End If
		End Sub

		Private Sub txtEHSTA_KeyPress(ByVal eventSender As System.Object, ByVal eventArgs As System.Windows.Forms.KeyPressEventArgs) Handles txtEHSTA.KeyPress
			Dim KeyAscii As Short = Asc(eventArgs.KeyChar)
			If KeyAscii = 13 Then txtEHDUE.Focus()
			eventArgs.KeyChar = Chr(KeyAscii)
			If KeyAscii = 0 Then
				eventArgs.Handled = True
			End If
		End Sub



		Private Sub txtEHVAT_KeyPress(ByVal eventSender As System.Object, ByVal eventArgs As System.Windows.Forms.KeyPressEventArgs) Handles txtEHVAT.KeyPress
			Dim KeyAscii As Short = Asc(eventArgs.KeyChar)

			If KeyAscii <> 13 Then
				GoTo EventExitSub
			End If
			If Not IsNumeric(txtEHVAT.Text) Then
				MsgBox("Gia tri nhap phai la so")
				txtEHVAT.Focus()
				GoTo EventExitSub
			End If
			If Val(txtEHVAT.Text) < 0 Then
				MsgBox("Gia tri nhap phai lon hon hay bang 0")
				txtEHVAT.Focus()
				GoTo EventExitSub
			End If
			If (Val(txtEHVAT.Text) / 100) > 1 Then
				MsgBox("Gia tri nhap phai nho hon 100")
				txtEHVAT.Focus()
				GoTo EventExitSub
			End If
			txtEHDUE.Focus()


EventExitSub:
			eventArgs.KeyChar = Chr(KeyAscii)
			If KeyAscii = 0 Then
				eventArgs.Handled = True
			End If
		End Sub

		Private Sub txtEHVAT_Leave(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles txtEHVAT.Leave

			If Not IsNumeric(txtEHVAT.Text) Then
				MsgBox("Gia tri nhap phai la so")
				Exit Sub
			End If
			If Val(txtEHVAT.Text) < 0 Then
				MsgBox("Gia tri nhap phai lon hon hay bang 0")
				'txtEHVAT.SetFocus
				Exit Sub
			End If
			If (Val(txtEHVAT.Text) / 100) > 1 Then
				MsgBox("Gia tri nhap phai nho hon 100")
				'txtEHVAT.SetFocus
				Exit Sub
			End If
			'txtEHDUE.SetFocus
		End Sub

		Private Sub txtELAMT_KeyPress(ByVal eventSender As System.Object, ByVal eventArgs As System.Windows.Forms.KeyPressEventArgs) Handles txtELAMT.KeyPress
			Dim KeyAscii As Short = Asc(eventArgs.KeyChar)
			If KeyAscii = 13 Then txtELVAM.Focus()
			eventArgs.KeyChar = Chr(KeyAscii)
			If KeyAscii = 0 Then
				eventArgs.Handled = True
			End If
		End Sub

		Private Sub txtELBCA_KeyPress(ByVal eventSender As System.Object, ByVal eventArgs As System.Windows.Forms.KeyPressEventArgs) Handles txtELBCA.KeyPress
			Dim KeyAscii As Short = Asc(eventArgs.KeyChar)
			If KeyAscii = 13 Then txtELBVA.Focus()
			eventArgs.KeyChar = Chr(KeyAscii)
			If KeyAscii = 0 Then
				eventArgs.Handled = True
			End If
		End Sub

		Private Sub txtELBVA_KeyPress(ByVal eventSender As System.Object, ByVal eventArgs As System.Windows.Forms.KeyPressEventArgs) Handles txtELBVA.KeyPress
			Dim KeyAscii As Short = Asc(eventArgs.KeyChar)
			If KeyAscii = 13 Then txtELAMT.Focus()
			eventArgs.KeyChar = Chr(KeyAscii)
			If KeyAscii = 0 Then
				eventArgs.Handled = True
			End If
		End Sub


		Private Sub txtELITN_KeyPress(ByVal eventSender As System.Object, ByVal eventArgs As System.Windows.Forms.KeyPressEventArgs) Handles txtELITN.KeyPress
			Dim KeyAscii As Short = Asc(eventArgs.KeyChar)
			On Error GoTo result_err
			Dim Result As String
			Dim RecIIM As ADODB.Recordset
			If KeyAscii = 13 Then
				Result = Find("IIM", "IMITD", "WHERE IMITN='" & txtELITN.Text & "'")
				If Result = "" Then
					AdoIIM.Refresh()
					cboELITN.Visible = True
					cboELITN.Focus()
					GoTo EventExitSub
				Else
					txtELITD.Text = Result
					txtELPRC.Text = Find("IIM", "IMPRC", "WHERE IMITN='" & txtELITN.Text & "'")
					cboELITN.Visible = False
					txtELQTY.Focus()
					cmdSave.Enabled = True
				End If
				'set lai khoi luong tuong ung
				If (Val(txtELPSQ.Text) <> 0) Or (Val(txtELQTY.Text) <> 0) Then
					txtELWGT.Focus()
					RecIIM = DBconn.Execute("Select IMWPU from IIM where IMITN='" & txtELITN.Text & "'")
					If RecIIM.EOF Then
						txtELWGT.Text = CStr(0)
						txtELWGT.SelectionLength = 1
						txtELWGT.SelectionStart = 0
						txtELWGT.SelectedText = CStr(1)
						txtELWGT.Focus()
						GoTo EventExitSub
					End If
					If IsDBNull(RecIIM.Fields("IMWPU").Value) Then
						txtELWGT.Text = CStr(0)
						txtELWGT.Focus()
					Else
						txtELPSW.Text = CStr(RecIIM.Fields("IMWPU").Value * CDbl(txtELPSW.Text))
						txtELWGT.Text = CStr(RecIIM.Fields("IMWPU").Value * CDbl(txtELQTY.Text))
						txtELPSQ.Focus()
					End If


				End If
			End If
result_next:
			GoTo EventExitSub
result_err:
			MsgBox(Err.Description)
			Resume result_next
EventExitSub:
			eventArgs.KeyChar = Chr(KeyAscii)
			If KeyAscii = 0 Then
				eventArgs.Handled = True
			End If
		End Sub
		Sub OpenFile()
			Dim DelStatus As Object
			Dim FieldDel As Object
			On Error GoTo Err_Openfile
			AdoRCM.ConnectionString = ConnectString
			AdoRCM.ConnectionTimeout = 10
			AdoRCM.CommandType = ADODB.CommandTypeEnum.adCmdText
			AdoRCM.RecordSource = "Select CMCNO,CMNME from RCM" & " ORDER BY CMNME"
			'AdoRCM.Refresh

			AdoAVM.ConnectionString = ConnectString
			AdoAVM.ConnectionTimeout = 10
			AdoAVM.CommandType = ADODB.CommandTypeEnum.adCmdText
			AdoAVM.RecordSource = "Select * from AVM"
			'AdoAVM.Refresh

			FieldDel = "IMRID"
			DelStatus = "IZ"
			SQL = "Select IMITN,IMITN +'  '+IMITD as IMITD  from IIM where " & FieldDel & " <>'" & DelStatus & "' "
			AdoIIM.ConnectionString = ConnectString
			AdoIIM.ConnectionTimeout = 10
			AdoIIM.CommandType = ADODB.CommandTypeEnum.adCmdText
			AdoIIM.RecordSource = SQL
			'AdoIIM.Refresh

			AdoECL.ConnectionString = ConnectString
			AdoECL.ConnectionTimeout = 10
			AdoECL.CommandType = ADODB.CommandTypeEnum.adCmdText
			AdoECL.RecordSource = "Select ECL.* from ECL " & " LEFT JOIN ECH ON ECL.ELODN=ECH.EHODN where ECH.EHODN ='" & txtEHODN.Text & "' ORDER BY ELOLN"
			'AdoECL.Refresh

			'AdoCCM.Refresh

			'Adodc6.ConnectionString = ConnectString
			'Adodc6.ConnectionTimeout = 10
			'Adodc6.CommandType = adCmdText
			'Adodc6.RecordSource = "Select * from ECH"
			'Adodc6.Refresh

			AdoSMM.ConnectionString = ConnectString
			AdoSMM.ConnectionTimeout = 10
			AdoSMM.CommandType = ADODB.CommandTypeEnum.adCmdText
			AdoSMM.RecordSource = "Select * from SMM"
			'AdoSMM.Refresh
			AdoRCM.ConnectionString = ConnectString
			AdoRCM.ConnectionTimeout = 10
			AdoRCM.CommandType = ADODB.CommandTypeEnum.adCmdText
			AdoRCM.RecordSource = "Select CMCNO,CMNME from RCM"


			' Create array from combobox Custtype,StatusOrder, ValuePer
			INVType(1) = "1 Normal"
			INVType(2) = "2 By pass Inventory"

			StaOrd(1) = "1 New"
			StaOrd(2) = "2 Printed"
			StaOrd(3) = "3 Confirmed"
			StaOrd(4) = "4 B/O"
			StaOrd(5) = "5 Caredit hold"
			StaOrd(6) = ""
			StaOrd(7) = "7 Canceled"
			Per(1) = "Unt"
			Per(2) = "Wgt"
Next_Openfile:
			Exit Sub
Err_Openfile:
			MsgBox(Err.Description)
			Resume Next_Openfile
		End Sub

		Sub ButtonStatus()
			On Error GoTo Err_action
			cmdAddNew.Enabled = Not ISEDITING
			cmdReview.Enabled = Not ISEDITING
			cmdNewItem.Enabled = Not ISEDITING
			cmdEdit.Enabled = Not ISEDITING
			Cmddelline.Enabled = Not ISEDITING
			cmdSave.Enabled = ISEDITING Or ISADDING
			cmdCancel.Enabled = ISEDITING Or ISADDING


			FrameBB.Enabled = ISEDITING Or ISADDING	'NewTrans
			FrameECLine.Enabled = ISEDITING Or ISADDING
Next_Action:
			Exit Sub
Err_action:
			MsgBox(Err.Description)
			Resume Next_Action

		End Sub

		Sub WriteToFile()
			Dim recECL As Object
			Dim Result As Object
			On Error GoTo ord500_err
			Dim msg, strSQL As Object
			'Kiem tra trang thai cua Order

			Result = Find("ECH", "EHSTT", "WHERE EHODN='" & txtEHODN.Text & "'")
			If Result <> "1" And Result <> "7" And Result <> "" Then
				MsgBox("Order nay da bi khoa khong the thay doi duoc")
				NewTrans = False
				ISADDING = False
				ISEDITING = False
				ButtonStatus()
				Exit Sub
			End If

			If NewTrans Then
				WritetoECH(("add"))
			End If
			'them mot mat hang moi vao trong Order
			If ISADDING Then
				WritetoECL(("add"))
				'WriteToIIM txtELITN, Val(txtELWGT), Val(txtELQTY)
			ElseIf ISEDITING Then  'Cap nhat du lieu
				If txtEHODN.Text <> OldEHODN Then
					msg = MsgBox(" Are you sure change the Order Number?", MsgBoxStyle.OkCancel, "Warning")
					If msg = MsgBoxResult.Cancel Then
						Exit Sub
					Else
						strSQL = "Update ECH set EHODN='" & txtEHODN.Text & "'" & " WHERE EHODN='" & OldEHODN & "'"
						'MsgBox strSQL
						DBconn.Execute(strSQL)
					End If
				End If
				WritetoECH(("upd"))
				WritetoECL(("upd"))
			End If
			Call UpdateNOLine()
			'recECL.Close
			recECL = DBconn.Execute("SELECT ELODN, SUM(ELVAM) AS SumELVAM, SUM(ELAMT)" & " AS SumELAMT, SUM(ELBVA) AS SumELBVA, SUM(ELBCA) " & " AS SumELBCA FROM ECL WHERE (ELODN = '" & txtEHODN.Text & "') GROUP BY ELODN")
			With recECL
				txtEHAMT.Text = CStr(System.Math.Round(!SumELAMT, 2))
				txtEHVAM.Text = CStr(System.Math.Round(!SumELVAM, 2))
				txtEHBCA.Text = CStr(System.Math.Round(!SumELBCA, 2))
				txtEHBVA.Text = CStr(System.Math.Round(!SumELBVA, 2))
			End With

			recECL.Close()

			DBconn.Execute("UPDATE ECH SET EHAMT=" & txtEHAMT.Text & ",EHVAM=" & txtEHVAM.Text & ",EHBAM=" & txtEHBCA.Text & ",EHBVA=" & txtEHBVA.Text & " WHERE EHODN='" & txtEHODN.Text & "'")
			txtEHAMT.Text = VB6.Format(txtELAMT.Text, "##,##0.00")
			txtEHVAM.Text = VB6.Format(txtELVAM.Text, "##,##0.00")
			txtEHBCA.Text = VB6.Format(txtELBCA.Text, "##,##0.00")
			txtEHBVA.Text = VB6.Format(txtELBVA.Text, "##,##0.00")
ord500_next:
			Exit Sub
ord500_err:
			MsgBox("Write to file Sub: " & Err.Description)
			Resume ord500_next
		End Sub

		Sub WriteToIIM(ByRef ITN As Object, ByRef WGT As Object, ByRef unit As Object)

			unit = IIf(IsNumeric(unit), unit, 0)
			WGT = IIf(IsNumeric(WGT), WGT, 0)


			SQL = "UPDATE IIM SET IMCOW=IMCOW+" & WGT & ",IMCOU=IMCOU+" & unit & " WHERE IMITN='" & ITN & "'"
			DBconn.Execute(SQL)

		End Sub


		Private Sub txtELITN_Leave(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles txtELITN.Leave
			On Error GoTo Err_action
			Dim Result As String
			Result = Find("IIM", "IMITD", "WHERE IMITN='" & txtELITN.Text & "'")
			If Result = "" Then
				MsgBox("Don't have this Item code")
				cboELITN.Visible = True
				'cboELITN.SetFocus
			Else

				txtELITD.Text = Result
				txtELPRC.Text = Find("IIM", "IMPRC", "WHERE IMITN='" & txtELITN.Text & "'")
				cboELITN.Visible = False
				'txtELVAT = VATRate(txtEHCNO, txtELITN)

			End If
			'Tinh lai khoi luong tuong ung
			Dim RecIIM As ADODB.Recordset
			If (Val(txtELPSQ.Text) <> 0) Or (Val(txtELQTY.Text) <> 0) Then
				'txtELWGT.SetFocus
				RecIIM = DBconn.Execute("Select IMWPU from IIM where IMITN='" & txtELITN.Text & "'")
				If Not RecIIM.EOF Then
					If IsDBNull(RecIIM.Fields("IMWPU").Value) Then
						txtELWGT.Text = CStr(0)
						txtELPSW.Text = CStr(0)
						'txtELWGT.SetFocus
					Else
						txtELPSW.Text = CStr(RecIIM.Fields("IMWPU").Value * CDbl(txtELPSW.Text))
						txtELWGT.Text = CStr(RecIIM.Fields("IMWPU").Value * CDbl(txtELQTY.Text))

					End If
				Else
					txtELWGT.Text = CStr(0)
				End If
			End If

Next_Action:
			Exit Sub
Err_action:
			MsgBox(Err.Description)
			Resume Next_Action

		End Sub

		Private Sub txtELPDC_KeyPress(ByVal eventSender As System.Object, ByVal eventArgs As System.Windows.Forms.KeyPressEventArgs) Handles txtELPDC.KeyPress
			Dim KeyAscii As Short = Asc(eventArgs.KeyChar)
			If KeyAscii = 13 Then txtELPRC.Focus()
			eventArgs.KeyChar = Chr(KeyAscii)
			If KeyAscii = 0 Then
				eventArgs.Handled = True
			End If
		End Sub

		Private Sub txtELPRC_KeyPress(ByVal eventSender As System.Object, ByVal eventArgs As System.Windows.Forms.KeyPressEventArgs) Handles txtELPRC.KeyPress
			Dim KeyAscii As Short = Asc(eventArgs.KeyChar)
			If KeyAscii <> 13 Then
				GoTo EventExitSub
			End If
			If Val(txtELPRC.Text) < 0 Then
				MsgBox("Gia nhap khong hop le")
				txtELPRC.Focus()
				GoTo EventExitSub
			Else
				cboELPER.Focus()
				Call RefeshLineAmount()
			End If



EventExitSub:
			eventArgs.KeyChar = Chr(KeyAscii)
			If KeyAscii = 0 Then
				eventArgs.Handled = True
			End If
		End Sub



		Private Sub txtELPRC_Leave(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles txtELPRC.Leave
			If Val(txtELPRC.Text) < 0 Then
				MsgBox("Gia nhap khong hop le")
				'txtELPRC.SetFocus
				Exit Sub
				End
				Call RefeshLineAmount()
			End If
		End Sub

		'Private Sub txtELPSQ_KeyPress(KeyAscii As Integer)
		'    If KeyAscii = 13 Then
		'        Dim RecIIM As Recordset
		'        Set RecIIM = DBconn.Execute("Select IMWPU from IIM where IMITN='" & txtELITN & "'")
		'        If Not RecIIM.EOF Then
		'            If IsNull(RecIIM!IMWPU) Then
		'                txtELPSW = ""
		'            Else
		'                txtELPSW = RecIIM!IMWPU * txtELPSQ
		'            End If
		'        Else
		'            txtELPSW = 0
		'        End If
		'        txtELPSW.SetFocus
		'    End If
		'End Sub

		'Private Sub txtELPSQ_LostFocus()
		'
		'        Dim RecIIM As Recordset
		'        Set RecIIM = DBconn.Execute("Select IMWPU from IIM where IMITN='" & txtELITN & "'")
		'        If Not RecIIM.EOF Then
		'            If IsNull(RecIIM!IMWPU) Then
		'                txtELPSW = ""
		'                txtELPSW.SetFocus
		'            Else
		'                txtELPSW = RecIIM!IMWPU * txtELPSQ
		'            End If
		'
		'        Else
		'            txtELPSW = 0
		'        End If
		'End Sub





		'Private Sub txtELPSW_KeyPress(KeyAscii As Integer)
		'    If KeyAscii = 13 Then
		'        txtELPDC.SetFocus
		'    End If
		'End Sub

		Private Sub txtELQTY_KeyPress(ByVal eventSender As System.Object, ByVal eventArgs As System.Windows.Forms.KeyPressEventArgs) Handles txtELQTY.KeyPress
			Dim KeyAscii As Short = Asc(eventArgs.KeyChar)
			On Error GoTo Err_action
			Dim RecIIM As ADODB.Recordset
			If KeyAscii = 13 Then

				RecIIM = DBconn.Execute("Select IMWPU from IIM where IMITN='" & txtELITN.Text & "'")
				If Not RecIIM.EOF Then
					If IsDBNull(RecIIM.Fields("IMWPU").Value) Then
						txtELWGT.Text = ""
						txtELWGT.Focus()
					Else
						txtELWGT.Text = CStr(RecIIM.Fields("IMWPU").Value * CDbl(txtELQTY.Text))
						txtELPDC.Focus()
					End If
				Else
					txtELWGT.Focus()
				End If
				Call RefeshLineAmount()
			End If

Next_Action:
			GoTo EventExitSub
Err_action:
			MsgBox(Err.Description)
			Resume Next_Action
EventExitSub:
			eventArgs.KeyChar = Chr(KeyAscii)
			If KeyAscii = 0 Then
				eventArgs.Handled = True
			End If
		End Sub

		Private Sub txtELQTY_Leave(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles txtELQTY.Leave
			On Error GoTo Err_action
			'txtELWGT.SetFocus
			Dim RecIIM As ADODB.Recordset
			RecIIM = DBconn.Execute("Select IMWPU from IIM where IMITN='" & txtELITN.Text & "'")
			If Not RecIIM.EOF Then
				If IsDBNull(RecIIM.Fields("IMWPU").Value) Then
					MsgBox("Nhap vao khoi luong tuong ung")
					txtELWGT.Text = CStr(0)
					'txtELWGT.SetFocus
				Else
					txtELWGT.Text = CStr(RecIIM.Fields("IMWPU").Value * CDbl(txtELQTY.Text))
					Call RefeshLineAmount()
					'txtELPDC.SetFocus
				End If
			Else
				MsgBox("Nhap vao khoi luong tuong ung")
				'txtELWGT.SetFocus
			End If

Next_Action:
			Exit Sub
Err_action:
			MsgBox(Err.Description)
			Resume Next_Action

		End Sub

		Private Sub txtELVAM_KeyPress(ByVal eventSender As System.Object, ByVal eventArgs As System.Windows.Forms.KeyPressEventArgs) Handles txtELVAM.KeyPress
			Dim KeyAscii As Short = Asc(eventArgs.KeyChar)
			If KeyAscii = 13 Then cmdSave.Focus()
			eventArgs.KeyChar = Chr(KeyAscii)
			If KeyAscii = 0 Then
				eventArgs.Handled = True
			End If
		End Sub


		Private Sub txtELWGT_KeyPress(ByVal eventSender As System.Object, ByVal eventArgs As System.Windows.Forms.KeyPressEventArgs) Handles txtELWGT.KeyPress
			Dim KeyAscii As Short = Asc(eventArgs.KeyChar)
			On Error Resume Next

			If KeyAscii = 13 Then
				If Val(txtELWGT.Text) <= 0 Then
					MsgBox("Gia tri khong hop le")
					txtELWGT.Focus()
				Else
					Call RefeshLineAmount()
					txtELPDC.Focus()
					Call RefeshLineAmount()
				End If
			End If

			eventArgs.KeyChar = Chr(KeyAscii)
			If KeyAscii = 0 Then
				eventArgs.Handled = True
			End If
		End Sub
		Private Sub txtELWGT_Leave(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles txtELWGT.Leave
			On Error Resume Next
			If Val(txtELWGT.Text) <= 0 Then
				MsgBox("Gia tri khong hop le")
				'txtELWGT.SetFocus
			Else
				Call RefeshLineAmount()
			End If

		End Sub
		Function ValidInput() As Object
			On Error GoTo ValidInput_err
			Dim Result As Object
			Dim StrErr As String
			Dim Havelot As Byte
			Havelot = False
			If Not IsDate(txtEHDTE.Text) Then StrErr = StrErr & Chr(13) & "* Wrong Invoice Date!"
			If Not IsDate(txtEHDUE.Text) Then StrErr = StrErr & Chr(13) & "* Wrong Due Date!"
			If Find("RCM", "CMCNO", "WHERE CMCNO='" & txtEHCNO.Text & "'") = "" Then StrErr = StrErr & Chr(13) & "* Not have Customer"

			If Find("CCM", "CMCCD", "WHERE CMCCD='" & cboEHCCD.BoundText & "'") = "" Then StrErr = StrErr & Chr(13) & "* Not have Currency code"


			If CDbl(txtELQTY.Text) = 0 Then StrErr = StrErr & Chr(13) & "* Quantity must be >0"
			If CDbl(txtELWGT.Text) = 0 Then StrErr = StrErr & Chr(13) & "* Weight must be >0"
			If StrErr <> "" Then
				MsgBox("Please check again!" & StrErr)
				ValidInput = False
			Else
				ValidInput = True
			End If
ValidInput_next:
			Exit Function
ValidInput_err:
			MsgBox("ValidInput Error : " & Err.Description)
			Resume ValidInput_next
		End Function
	End Class
End Namespace